package rjp.map;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import gnu.trove.map.hash.TByteObjectHashMap;
import gnu.trove.map.hash.TByteFloatHashMap;
import gnu.trove.map.hash.TIntFloatHashMap;
import gnu.trove.map.hash.TIntObjectHashMap;

public class CachingSantaMap extends SantaMap {

	Map<Integer, Map<Integer, Float>> distances;
	Map<Integer, List<DistCity>> closest;
	private int cacheSize;
	private List<Boolean> fullCache;
	
	public CachingSantaMap(String fn, int cacheSize) throws IOException {
		super(fn);
		this.cacheSize=cacheSize;
		this.distances=new HashMap<Integer, Map<Integer, Float>>(this.size());
		this.closest=new HashMap<Integer, List<DistCity>>();
		this.fullCache=new ArrayList<Boolean>(this.size());
		for(int i=0;i<this.size();i++)
		{
			fullCache.add(false);
		}
	}
	
	
	
	@Override
	public int getClosestInSet(int index, Set<Integer> allowed)
	{
		this.fillCache(index);
		for(int c=0; c<this.cacheSize; c++)
		{
			DistCity dc=this.closest.get(index).get(c);
			if(allowed.contains(dc.getCity()))
			{
				return dc.getCity();
			}
		}
		return super.getClosestInSet(index, allowed);
	}
	
	@Override
	public int getClosestInSet(int index, Set<Integer> allowed, Set<Integer> notAllowed) throws Exception
	{

		this.fillCache(index);
		for(int c=0; c<this.cacheSize; c++)
		{
//			if(index==0 && c==1)
//			{
//				System.out.println("asdf");
//			}
			
			//System.out.println(index+" "+c+" "+this.closest.get(index).size());
			DistCity dc=this.closest.get(index).get(c);
			
			if(allowed.contains(dc.getCity()) && !notAllowed.contains(dc.getCity()) )
			{
				return dc.getCity();
			}
		}
		return super.getClosestInSet(index, allowed, notAllowed);
	}
	
	private void fillCache(int city)
	{
		if(this.fullCache.get(city))
		{
			return;
		}
		
		for(int i=0;i<this.size(); i++)
		{
			this.getDistance(city, i);
		}
		this.fullCache.set(city, true);
	}
	
	private class DistCity	{
		float dist;
		int city;
		public DistCity(float dist, int city)
		{
			this.dist=dist;
			this.city=city;
		}
		
		public float getDist()
		{
			return dist;
		}
		
		public int getCity()
		{
			return this.city;
		}
	}


	@Override
	//cache n closest cities
	public float getDistance(int indexA, int indexB) {
		int a=Math.max(indexA, indexB);
		int b=Math.min(indexA, indexB);
		
		if (!distances.containsKey(a)){
			distances.put(a, new HashMap<Integer, Float>(this.cacheSize));
			List<DistCity> l=new LinkedList<DistCity>();
			closest.put(a, l);
		}
		if (!distances.containsKey(b)){
			distances.put(a, new HashMap<Integer, Float>(this.cacheSize));
			List<DistCity> l=new LinkedList<DistCity>();
			closest.put(b, l);
		}
		
		if (distances.get(a).containsKey( b)){
			return distances.get(a).get(b);
		} 
		
		float dist=super.getDistance(a, b);
		updateCache(a, b, dist);
		updateCache(b, a, dist);
		
		return dist;
	}



	private void updateCache(int a, int b, float dist) {
		boolean addToCache=false;
		if(closest.get(a).size()<this.cacheSize)
		{
			addToCache=true;
		} else
		{
			if(closest.get(a).get(closest.get(a).size()-1).getDist() > dist )
			{
				addToCache=true;
			}
		}
		if(addToCache)
		{
			int i=0;
			while(i<closest.get(a).size())
			{
				if(closest.get(a).get(i).getDist()>dist)
				{
					break;
				}
				i++;
			}
			closest.get(a).add(i, new DistCity(dist, b));
		}
		
		if(closest.get(a).size()>this.cacheSize)
		{
			distances.get(a).remove(closest.get(a).get(this.cacheSize).getCity());
			closest.get(a).remove(this.cacheSize);
		}
		
//		if(closest.get(a).size()<20)
//		{
//			System.out.println(a+ " "+b+" "+dist+" "+closest.get(a).size());
//		}
	}

}



